public class NewBinarySearch {
    public static void main(String[] args) {
        int[] arr = {1,2,3,3,3,3,5,6,7};
        int target = 3;
        System.out.print(findLeft(arr, 3)+" ");
        System.out.println(findRight(arr, 3));
    }
    public static int findLeft(int[] arr,int target){
        int left = 0;
        int right = arr.length - 1;
        int sign = -1;
        while (left <= right){
            int mid = (left + right) >>> 1;

            if (arr[mid] < target){
                left = mid + 1;
            } else if (target < arr[mid]) {
                right = mid - 1;
            }else {
                sign = mid;
                right = mid - 1;
            }
        }
        return sign;
    }

    public static int findRight(int[] arr, int target){
        int left = 0;
        int right = arr.length - 1;
        int sign = -1;
        while (left <= right){
            int mid = (left + right) >>> 1;
            if (arr[mid] < target){
                left = mid + 1;
            } else if (target < arr[mid]) {
                right = mid - 1;
            }else {
                sign = mid;
                left = mid + 1;
            }
        }
        return sign;
    }
}

/*    public static void main(String[] args) {
        int[] arr = {1,3,5,7,9,11};
        int target = 2;
        System.out.println(search(arr, target));
    }
    public static int search(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (left + right) >>> 1;
            if (arr[mid] < target) {
                left = mid + 1;
            } else if (target < arr[mid]) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return -(left + 1);
    }
}*/
    /*    public static void main(String[] args) {
        int[] arr = {1,3,5,7,9,11};
        int target = 3;
        System.out.println(search(arr, target));
    }
    public static int search(int[] arr, int target) {
        int left = 0;
        int right = arr.length;
        while (left < right) {
            int mid = (left + right) >>> 1;
            if (arr[mid] < target) {
                left = mid + 1;
            } else if (target < arr[mid]) {
                right = mid;
            } else {
                return mid;
            }
        }
        return -1;
    }


}
/*    public static void main(String[] args) {
        int[] arr = {1,3,5,7,9,11};
        int target = 3;
        System.out.println(search(arr, target));
    }
    public static int search (int[] arr, int target){
        int left = 0;
        int right = arr.length;
        while (1 < right - left){
            int mid = (left + right) >>> 1;
            if (target < arr[mid]){
                right = mid;
            } else  {
                left = mid;
            }
        }
        if (arr[left] == target){
            return left;
        }else {
            return -1;
        }

    }
}*/
/*    public static void main(String[] args) {
        int[] arr = {1,3,5,7,9,11};
        int target = 3;
        System.out.println(search(arr, target));
    }
    public static int search(int[] arr, int target) {
        int left = 0;
        int right = arr.length;
        while (left < right) {
            int mid = (left + right) >>> 1;
            if (arr[mid] < target) {
                left = mid + 1;
            } else if (target < arr[mid]) {
                right = mid;
            } else {
                return mid;
            }
        }
        return -1;
    }
}*/
